package com.easy.service.impl;

import com.easy.bean.User;
import com.easy.mapper.UserDao;
import com.easy.service.UserService;
import com.easy.util.Page;
import com.easy.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Member;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    private static final String CACHE_NAME = "user";

    @Autowired
    UserDao userDao;

    @Override
    public boolean updatePassword(Integer userId, String oldPassword, String newPassword){
        User user = userDao.one(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        if (!PasswordUtil.checkPassword(oldPassword, user.getPassword())) {
            throw new RuntimeException("旧密码错误");
        }
        String encryptedPassword = PasswordUtil.getEncryption(newPassword);
        user.setPassword(encryptedPassword);
        return userDao.edit(user) > 0;
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'user:username:' + #p0.username", condition = "#result != null")
    public User add(User user) {
        // 检查用户名是否已存在
        User existingUserByUsername = userDao.getByName(user.getUsername());
        if (existingUserByUsername != null) {
            throw new RuntimeException("用户名已存在");
        }

        // 检查邮箱是否已存在
        User existingUserByEmail = userDao.getByEmail(user.getEmail());
        if (existingUserByEmail != null) {
            throw new RuntimeException("邮箱已被注册");
        }

        // 对密码进行加密处理
        String encryptedPassword = PasswordUtil.getEncryption(user.getPassword());
        user.setPassword(encryptedPassword);

        //新增数据
        userDao.add(user);
        //通过主键查询数据库的数据
        User result =userDao.one(user.getId());
        return result;
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'user:id:' + #p0")
    public int del(Integer id) {
        return userDao.del(id);
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'user:' + #p0.id", condition = "#p0 != null and #p0.id != null")
    public User edit(User user) {
        userDao.edit(user);
        User result=userDao.one(user.getId());
        return result;
    }

    @Override
    @Cacheable(value = CACHE_NAME, key = "'user:list:' + T(java.util.Objects).hash(#p0)", unless = "#result == null or #result.isEmpty()")
    public List<User> all(User user) {
        return userDao.all(user);
    }

    @Override
  @Cacheable(value = CACHE_NAME, key = "'user:id:' + #p0", unless = "#result == null")
    public User one(Integer id) {
        return userDao.one(id);
    }

    @Override
    public void page(Page page, User user){
        int count=userDao.pageCount(user);
        Object data=userDao.page(page,user);
        page.setCount(count);
        page.setData(data);
    }
}
